package com.cogent.validator.impl;

import java.util.HashMap;

import com.cogent.common.util.MapUtil;
import com.cogent.common.util.StringUtil;
import com.cogent.validator.IValidator;
import com.cogent.validator.domain.ValidatorDomain;

public class TextAreaRepeatCheckValidator implements IValidator{
	public String getScript(ValidatorDomain domain) {
		StringBuffer str = new StringBuffer();
		HashMap curMap = domain.getKeypairmap();
		String temp = domain.getKey() + " 不能重复，当前重复数据为：";
		if(!StringUtil.isEmpty(domain.getNotemsg()))
			temp = domain.getNotemsg();
		str.append("textAreaRepeatCheck(\"");
		if(!StringUtil.isEmpty(domain.getPrefix()))
			str.append(domain.getPrefix()).append("\" + curnum + \"]" + domain.getInfix() + "\" + \"");
		str.append(domain.getProperty());
		str.append("\",\"").append(temp).append("\",errmsg").append(");");
		
		return str.toString();
	}
	
	public String validator(HashMap fromMap, ValidatorDomain domain) {
		String ret = "";
		HashMap curMap = domain.getKeypairmap();
		String value = MapUtil.getStringValue(fromMap, domain.getProperty());
		String[] temps = value.split("\r\n");
		HashMap ht = new HashMap();
		for(int i=0; i<temps.length; i++) {
			if(ht.containsKey(temps[i]))
				ret = domain.getKey() + " 不能重复，当前重复数据为：" + temps[i] + "!";
			else
				ht.put(temps[i], "");
		}
		
		return ret;
	}
}
